﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Effect;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Gameplay.PVE.Survivor
{
    /// <summary>
    /// 战斗中的特效代理器
    /// </summary>
    public class BattleEffectObject : BattleGameObject
    {
        public PveEffectController effectController;

        protected override void OnGameObjectCreatedInternal(GameObject gameObject)
        {
            effectController = gameObject.GetComponent<PveEffectController>();
        }
        
        protected override void OnGameObjectReadyInternal(GameObject gameObject)
        {
            if (effectController)
            {
                effectController.SetActive(true);
            }
            else
            {
                if (BattleDebug.enableLog)
                { 
                    BattleDebug.LogError("effectController is null " + gameObject.name);     
                }
            }

            if (isChain && chainTarget1 && chainTarget2)
            {
                SetChainTargetsInternal();
            }
            else if (isLine && chainTarget1 && chainTarget2)
            {
                RefreshLineTargets();
            }
        }
        
        protected override void OnGameObjectReleaseInternal()
        {
            if (effectController)
            {
                effectController.SetActive(false);
            }

            if (isChain)
            {
                var chain = effectController.GetComponentInChildren<ChainEffect>();
                if (chain)
                {
                    chain.Stop();
                }
            }
            chainTarget1 = null;
            chainTarget2 = null;
            isChain = false;
            isLine = false;
        }

        protected override void OnGameObjectDestroyInternal()
        {

        }

        private Transform chainTarget1;
        private Transform chainTarget2;
        private bool isLine;
        private bool isChain;
        
        public void SetChainTargets(Transform target1, Transform target2)
        {
            isChain = true;
            chainTarget1 = target1;
            chainTarget2 = target2;
            if (IsReady)
            {
                SetChainTargetsInternal();
            }
        }

        public void SetLineTargets(Transform target1, Transform target2)
        {
            isLine = true;
            chainTarget1 = target1;
            chainTarget2 = target2;
            if (IsReady)
            {
                RefreshLineTargets();
            }
        }

        public void RefreshLineTargets()
        {
            if (!IsReady)
            {
                return; 
            }
            //LocalEulerAngle = Vector3.zero;
            Forward = chainTarget2.position - chainTarget1.position;
            effectController.SetLineRendererLength(Vector3.Distance(chainTarget1.position, chainTarget2.position));
        }
        
        private void SetChainTargetsInternal()
        {
            var chain = effectController.GetComponentInChildren<ChainEffect>();
            if (chain)
            {
                chain.Play(chainTarget1,chainTarget2);
            }
        }

        public float GetExistTime()
        {
            if (effectController == null)
            {
                return 1;
            }

            if (effectController.IsLoop)
            {
                return 99999;
            }
            else
            {
                return effectController.PlayTime;
            }
        }
    }
}
